package com.zy.qudadid.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.zy.lcpassenger.R;
import com.zy.qudadid.presenter.LoginPresenter;
import com.zy.qudadid.ui.activity.base.ToolBarActivity;
import com.zy.qudadid.ui.view.LoginView;
import com.zy.qudadid.utils.AESCrypt;
import com.zy.qudadid.utils.StringUtil;
import com.zy.qudadid.utils.UserUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import butterknife.Bind;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by ASUS on 2017/2/15.
 */

public class LoginActivity extends ToolBarActivity<LoginPresenter> implements LoginView{
    @Bind(R.id.et_username)
    EditText et_username;
    @Bind(R.id.et_password)
    EditText et_password;

    @Override
    protected CharSequence provideTitle() {
        return "";
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_login;
    }

    @Override
    public void initListeners() {

    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @OnClick({R.id.btn_login,R.id.btn_register,R.id.btn_signin_code})
    protected void btn_OnClick(View view){
        switch (view.getId()){
            case R.id.btn_login:
                /*登录*/
            //    startActivity(MainActivity.class);
             //   startActivity(StrokeActivity.class);

                String phoneNumber = et_username.getText().toString();
                String pass = et_password.getText().toString();

                if (StringUtil.isEmpty(phoneNumber)) {
                    toast("手机号不能为空!");
                } else if (StringUtil.isEmpty(pass)) {
                    toast("密码不能为空!");
                }else {
                    Map map = new HashMap();
                    map.put("tel", phoneNumber);
                    map.put("pwd", pass);
                    String json = StringUtil.toJson(map);
                    try {
                        String data1 = AESCrypt.getInstance().encrypt(json);
                        String d = StringUtil.replaceBlank(data1);
                        presenter.Login(d);
                    } catch (Exception ex) {
                    }
                }
                break;
            case R.id.btn_register:
                /*注册*/
                startActivity(RegisterActivity.class);
                break;
            case R.id.btn_signin_code:
                /*忘记密码*/
                startActivity(NewForgetPasswordActivity.class);
                break;
        }
    }

    @Override
    public void loginSuccess() {

        JPushInterface.setAlias(getBaseContext(), "xianFengCustom_" + new UserUtil(getBaseContext()).getUserId(), new TagAliasCallback() {
            @Override
            public void gotResult(int i, String s, Set<String> set) {
                String s1 = s;
            }
        });

        startActivity(MainActivity.class);
    }

    @Override
    public void loginError(String msg) {
        toast(msg);
    }

    @Override
    public void exception() {
        toast("登录失败");
    }

    @Override
    protected void initThings(Bundle savedInstanceState) {
        super.initThings(savedInstanceState);
        if (new UserUtil(this).isLogin()) {
            startActivity(MainActivity.class);
            finish();
        }
    }
}
